﻿using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace mania4key_v2
{
    [Serializable]
    class OsuMap
    {
        //key
        public int bmid = -1;
        public int bmsetid = -1;
        //metadata
        public string path = "";
        public string filename = "";
        public string audiofilename = "";
        public int audiopreview = 0;
        public int audioleadin = 0;
        public int countdown = 0;
        public int mode = 3;
        public float keyamount = 0;
        public string title = "";
        public string titleunicode = "";
        public string artist = "";
        public string artistunicode = "";
        public string creator = "";
        public string version = "";
        public float difficulty = 0;
        public string background = "";
        public string video = "";
        public int defaultsampleset = 0;
        //list
        public List<OsuNotes> notes = new List<OsuNotes>();
        public List<OsuTimingPoints> timing = new List<OsuTimingPoints>();
       
        public void LoadNotes()
        {
            StreamReader s = new StreamReader(this.path);
            string parseme = "";
            while (s.Peek() >= 0)
            {
                parseme = s.ReadLine();
                if (parseme.StartsWith("[HitObjects]"))
                {
                    while (s.Peek() >= 0)
                    {
                        parseme = s.ReadLine();
                        var arr = parseme.Split(',');
                        if (arr[3] == "128")
                            notes.Add(new OsuNotes(float.Parse(arr[2]), float.Parse(arr[5].Split(':')[0]), int.Parse(arr[0]), int.Parse(arr[4]), int.Parse(arr[5].Split(':')[1]), int.Parse(arr[5].Split(':')[3]), arr[5].Split(':')[4]));
                        else
                            notes.Add(new OsuNotes(float.Parse(arr[2]), 0, int.Parse(arr[0]), int.Parse(arr[4]), int.Parse(arr[5].Split(':')[1]), int.Parse(arr[5].Split(':')[3]), arr[5].Split(':')[4]));
                        /*
                        parseme = s.ReadLine();
                        var arr = parseme.Split(',');
                        bool hold = false;
                        var arr2 = parseme.Split('|');
                        if (arr2.Length != 0) hold = true;
                        notes.Add(new OsuNotes(int.Parse(arr[2]), int.Parse(arr[0]), int.Parse(arr[1]), hold));
                        */
                    }
                }
            }
            s.Close();
        }

        public void LoadNotesSample()
        {
          
        }

        public OsuMap(string f)
        {
            this.path = f;
            StreamReader s = new StreamReader(f);
            while (s.Peek() >= 0)
            {
                string parseme = s.ReadLine();
                if (parseme.StartsWith("AudioFilename: "))
                {
                    audiofilename = parseme.Split(':')[1].Remove(0, 1);
                }
                if (parseme.StartsWith("PreviewTime: "))
                {
                    audiopreview = int.Parse(parseme.Split(':')[1].Remove(0, 1));
                }
                if (parseme.StartsWith("AudioLeadIn: "))
                {
                    audioleadin = int.Parse(parseme.Split(':')[1].Remove(0, 1));
                }
                if (parseme.StartsWith("Countdown: "))
                {
                    countdown = int.Parse(parseme.Split(':')[1].Remove(0, 1));
                }
                if (parseme.StartsWith("Mode: "))
                {
                    mode = int.Parse(parseme.Split(':')[1].Remove(0, 1));
                }
                if (parseme.StartsWith("Title:"))
                {
                    title = parseme.Split(':')[1];
                }
                if (parseme.StartsWith("TitleUnicode:"))
                {
                    titleunicode = parseme.Split(':')[1];
                }
                if (parseme.StartsWith("Artist:"))
                {
                    artist = parseme.Split(':')[1];
                }
                if (parseme.StartsWith("ArtistUnicode:"))
                {
                    artistunicode = parseme.Split(':')[1];
                }
                if (parseme.StartsWith("Creator:"))
                {
                    creator = parseme.Split(':')[1];
                }
                if (parseme.StartsWith("Version:"))
                {
                    version = parseme.Split(':')[1];
                }
                if (parseme.StartsWith("CircleSize:"))
                {
                    keyamount = float.Parse(parseme.Split(':')[1]);
                }
                if (parseme.StartsWith("BeatmapID:"))
                {
                    bmid = int.Parse(parseme.Split(':')[1]);
                }
                if (parseme.StartsWith("BeatmapSetID:"))
                {
                    bmsetid = int.Parse(parseme.Split(':')[1]);
                }
                if (parseme.StartsWith("SampleSet: "))
                {
                    var sampleset = parseme.Split(':')[1].Remove(0, 1);
                    if (sampleset == "Normal")
                        this.defaultsampleset = 1;
                    if (sampleset == "Soft")
                        this.defaultsampleset = 2;
                    if (sampleset == "Drum")
                        this.defaultsampleset = 3;
                }
                if (parseme.StartsWith("//Background and Video events"))
                {
                    parseme = s.ReadLine();
                    try
                    {
                        background = parseme.Split(',')[2].Replace("\"", "");
                    } catch
                    {

                    }
                }
            }
            s.Close();
        }
    }
}
